package cn.shop.admin.controller;

import cn.hutool.core.lang.Dict;
import cn.shop.admin.service.BackPaymentChannelService;
import cn.shop.base.MyPage;
import cn.shop.transcation.backPaymentChannelModule.entity.BackPaymentChannel;
import cn.shop.transcation.backPaymentChannelModule.entity.SetDefaultReq;
import cn.shop.utils.ApiResult;
import cn.shop.utils.enums.RechargeSolution;
import cn.shop.validation.Save;
import cn.shop.validation.Update;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 支付通道(BackPaymentChannel)表控制层
 *
 * @author jzw
 * @since 2022-02-24 09:51:38
 */
@Validated
@RestController
@RequestMapping("/api/v1/backPaymentChannel")
public class BackPaymentChannelController {

	/**
	 * 服务对象
	 */
	@Resource
	private BackPaymentChannelService backPaymentChannelService;

	/**
	 * 查询所有数据
	 *
	 * @param backPaymentChannel 查询实体
	 * @return 所有数据
	 * @author jzw
	 * @since 2022-02-24 09:51:38
	 */
	@GetMapping("queryAll")
	public ApiResult<List<BackPaymentChannel>> queryAll(Boolean isGroup, BackPaymentChannel backPaymentChannel, Integer channel) {
		LambdaQueryWrapper<BackPaymentChannel> queryWrapper = Wrappers.lambdaQuery(backPaymentChannel);
		if (channel == null) {
			queryWrapper.eq(BackPaymentChannel::getChannel, 0);
		} else {
			queryWrapper.eq(BackPaymentChannel::getChannel, channel);
		}
		if (isGroup != null && isGroup) {
			queryWrapper.groupBy(BackPaymentChannel::getMerchantNumber);
			queryWrapper.groupBy(BackPaymentChannel::getSecret);
		}
		return ApiResult.success(backPaymentChannelService.list(queryWrapper));
	}

	/**
	 * 分页查询所有数据
	 *
	 * @param page               分页对象
	 * @param backPaymentChannel 查询实体
	 * @return 所有数据
	 * @author jzw
	 * @since 2022-02-24 09:51:38
	 */
	@GetMapping("queryByPage")
	public ApiResult<MyPage<BackPaymentChannel>> queryByPage(MyPage<BackPaymentChannel> page,
	                                                         BackPaymentChannel backPaymentChannel) {
		return ApiResult.success(backPaymentChannelService.page(page, Wrappers.lambdaQuery(backPaymentChannel)));
	}

	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 * @author jzw
	 * @since 2022-02-24 09:51:38
	 */
	@GetMapping("queryOne/{id}")
	public ApiResult<BackPaymentChannel> selectOne(@PathVariable Serializable id) {
		return ApiResult.success(backPaymentChannelService.getById(id));
	}

	/**
	 * 通过主键查询单条数据
	 *
	 * @return 单条数据
	 * @author jzw
	 * @since 2022-02-24 09:51:38
	 */
	@GetMapping("queryDefaultChannel")
	public ApiResult<Dict> queryDefaultChannel(Integer channel) {
		Dict dict = Dict.create();
		if (channel == null || channel == 0) {
			dict.set("wx", backPaymentChannelService.getOne(Wrappers.lambdaQuery(BackPaymentChannel.class)
					.eq(BackPaymentChannel::getType, RechargeSolution.WX_PAYMENT)
					.eq(BackPaymentChannel::getChannel, 0)
					.eq(BackPaymentChannel::getDefaultChannel, true)));
			dict.set("ali", backPaymentChannelService.getOne(Wrappers.lambdaQuery(BackPaymentChannel.class)
					.eq(BackPaymentChannel::getType, RechargeSolution.ALIPAY_PAYMENT)
					.eq(BackPaymentChannel::getChannel, 0)
					.eq(BackPaymentChannel::getDefaultChannel, true)));
			dict.set("bank", backPaymentChannelService.getOne(Wrappers.lambdaQuery(BackPaymentChannel.class)
					.eq(BackPaymentChannel::getType, RechargeSolution.UPAY_PAYMENT)
					.eq(BackPaymentChannel::getChannel, 0)
					.eq(BackPaymentChannel::getDefaultChannel, true)));
		} else {
			dict.set("wx", backPaymentChannelService.getOne(Wrappers.lambdaQuery(BackPaymentChannel.class)
					.eq(BackPaymentChannel::getType, RechargeSolution.H5_WX)
					.eq(BackPaymentChannel::getChannel, channel)
					.eq(BackPaymentChannel::getDefaultChannel, true)));
			dict.set("ali", backPaymentChannelService.getOne(Wrappers.lambdaQuery(BackPaymentChannel.class)
					.eq(BackPaymentChannel::getType, RechargeSolution.H5_ALI)
					.eq(BackPaymentChannel::getChannel, channel)
					.eq(BackPaymentChannel::getDefaultChannel, true)));
			dict.set("bank", backPaymentChannelService.getOne(Wrappers.lambdaQuery(BackPaymentChannel.class)
					.eq(BackPaymentChannel::getType, RechargeSolution.H5_UPAY)
					.eq(BackPaymentChannel::getChannel, channel)
					.eq(BackPaymentChannel::getDefaultChannel, true)));
		}
		return ApiResult.success(dict);
	}

	/**
	 * 新增数据
	 *
	 * @param backPaymentChannel 实体对象
	 * @return 新增结果
	 * @author jzw
	 * @since 2022-02-24 09:51:38
	 */
	@PostMapping("insert")
	public ApiResult<Map<String, Object>> insert(@Validated(Save.class) @RequestBody BackPaymentChannel backPaymentChannel) {
		return ApiResult.result(backPaymentChannelService.save(backPaymentChannel));
	}

	/**
	 * 修改数据
	 *
	 * @param backPaymentChannel 实体对象
	 * @return 修改结果
	 * @author jzw
	 * @since 2022-02-24 09:51:38
	 */
	@PutMapping("update")
	public ApiResult<Map<String, Object>> update(@Validated(Update.class) @RequestBody BackPaymentChannel backPaymentChannel) {
		return ApiResult.result(backPaymentChannelService.updateById(backPaymentChannel));
	}

	/**
	 * 修改数据
	 *
	 * @return 修改结果
	 * @author jzw
	 * @since 2022-02-24 09:51:38
	 */
	@PutMapping("setDefault")
	public ApiResult<Map<String, Object>> setDefault(@Validated(Update.class) @RequestBody SetDefaultReq setDefaultReq) {
		if (setDefaultReq.getChannel() == null) {
			setDefaultReq.setChannel(0);
		}
		backPaymentChannelService.lambdaUpdate().set(BackPaymentChannel::getDefaultChannel, false)
				.eq(BackPaymentChannel::getChannel, setDefaultReq.getChannel())
				.update();
		backPaymentChannelService.lambdaUpdate().set(BackPaymentChannel::getDefaultChannel, true)
				.eq(BackPaymentChannel::getChannel, setDefaultReq.getChannel())
				.in(BackPaymentChannel::getId, setDefaultReq.getAlipay(), setDefaultReq.getBankpay(), setDefaultReq.getWxpay())
				.update();
		return ApiResult.success();

	}

	/**
	 * 删除单条数据
	 *
	 * @param id 主键
	 * @return 删除结果
	 * @author jzw
	 * @since 2022-02-24 09:51:38
	 */
	@DeleteMapping("delete/{id}")
	public ApiResult<Map<String, Object>> delete(@PathVariable Long id) {
		BackPaymentChannel backPaymentChannel = new BackPaymentChannel();
		backPaymentChannel.setId(id);
		return ApiResult.result(backPaymentChannelService.removeById(backPaymentChannel));
	}

	/**
	 * 批量删除数据
	 *
	 * @param idList 主键结合
	 * @return 删除结果
	 * @author jzw
	 * @since 2022-02-24 09:51:38
	 */
	@DeleteMapping("batchDelete")
	public ApiResult<Map<String, Object>> batchDelete(@RequestParam("idList") List<Long> idList) {
		return ApiResult.result(backPaymentChannelService.batchDelete(idList, new BackPaymentChannel()));
	}

}

